package com.kangye.enterprise.controller;

import com.kangye.enterprise.entity.Banner;
import com.kangye.enterprise.entity.User;
import com.kangye.enterprise.service.BannersService;
import com.kangye.enterprise.util.RedisStorageUtil;
import com.kangye.enterprise.util.UploadFileUtil;
import com.kangye.enterprise.util.ViewJsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @program: enterprise
 * @description: 轮播图
 * @author: 索腾龙最帅
 * @create: 2021-06-07 15:09
 **/
@Controller
public class BannersController {
    @Autowired
    private BannersService bannersService;

    //跳转广告页面
    @RequestMapping("/banners")
    public String selBanners() {
        //返回页面
        return "banners";
    }

    //查询广告表
    @ResponseBody
    @RequestMapping("/selBanners")
    public ViewJsonObject<Banner> selBanner(Integer page, Integer limit) {
        return bannersService.selBanner(page, limit);
    }

    //跳转广告添加页面
    @RequestMapping("/addBanner")
    private String addBanner() {
        return "addBanner";
    }

    //图片上传
    @ResponseBody
    @RequestMapping(value = "/imgUpload")
    public ViewJsonObject addBanners(HttpSession httpSession,MultipartFile... file) throws IOException {
        //遍历多个文件(文件上传)
        //for (MultipartFile f : file) {
        //文件的名字
        //String originalFilename = f.getOriginalFilename();
        //System.err.println(originalFilename + "图片名");
        //图片上传到指定的位置,返回一个文件的新名字,用于插入到数据库
        //String fileNewName = UploadFileUtil.uploadFilePro("filepath.properties", f);
        //System.out.println("开始图片名字"+originalFilename + "...." + "新名字"+fileNewName);
        //设置文件名字
        //meeting.setMeetingName(originalFilename);
        ////设置文件路径地址
        // meeting.setMeetingPath(fileNewName);
        //判断用户提交文件
        //meeting.setMeetingUserid(user.getUserId());
        //判断用户单位
        //meeting.setMeetingCompanyid(user.getUserCompanyId());
        //}
        //获取图片的名字,把图片存到指定的路径里
        String imgName = UploadFileUtil.uploadFilePro("filePath.properties", file);
        //将图片的名字放入到session中
        httpSession.setAttribute("imgName","http://localhost:8080/images/"+imgName);
        return new ViewJsonObject(0,"上传成功");

    }
    //添加轮播图
    @ResponseBody
    @RequestMapping(value = "/addBanners")
    public String addBanner(Banner banner,HttpSession httpSession){
        //从reds中获取上传的图片名字
        String imgName = (String) httpSession.getAttribute("imgName");
        //添加图片
        banner.setBannerUrl(imgName);
        //接收Service里的方法
        Integer code = bannersService.addBanners(banner);
        if (code>0){
            return "200";
        }else {
            return "201";
        }
    }
    //删除
    @ResponseBody
    @RequestMapping("/delBanners")
    public ViewJsonObject<Banner> delBanners(Integer id){
        //返回Service方法
        return bannersService.delBanners(id);
    }
    //根据id查询数据
    @ResponseBody
    @RequestMapping("/selBannersById")
    public ViewJsonObject<Banner> selBannersById(Integer id){
        //返回Service方法
        return bannersService.selBannersById(id);
    }



    //跳转轮播图页面
    @RequestMapping("/lbtu")
    public String selBanners1() {
        return "lbtu";
    }

    //查询轮播图
    @ResponseBody
    @RequestMapping("/selIbtu")
    public ViewJsonObject<Banner> lbtu(Integer page, Integer limit) {
        return bannersService.selIbtu(page, limit);
    }

    //跳转轮播图添加页面
    @RequestMapping("/addlbtu")
    private String addBanner1() {
        return "addlbtu";
    }

    //图片上传
    @ResponseBody
    @RequestMapping(value = "/imgUploads")
    public ViewJsonObject addBannerss(HttpSession httpSession,MultipartFile... file) throws IOException {
        //获取图片的名字,把图片存到指定的路径里
        String imgName = UploadFileUtil.uploadFilePro("filePath.properties", file);
        //将图片的名字放入到session中
        httpSession.setAttribute("imgName","http://localhost:8080/images/"+imgName);
        return new ViewJsonObject(0,"上传成功");

    }
    //添加轮播图
    @ResponseBody
    @RequestMapping(value = "/addlbtus")
    public String addBanners(Banner banner,HttpSession httpSession){
        //从session中获取上传的图片名字
        String imgName = (String) httpSession.getAttribute("imgName");
        //添加图片
        banner.setBannerUrl(imgName);
        //接收Service里的方法
        Integer code = bannersService.addIbtu(banner);
        if (code>0){
            return "200";
        }else {
            return "201";
        }
    }
    //修改
    @ResponseBody
    @RequestMapping(value = "/updIbtu")
    public String updIbtu(Banner banner){
        Integer code = bannersService.updIbtu(banner);
        if (code>0){
            return "200";
        }else {
            return "201";
        }
    }
}

